package com.yonyou.pmclouds.team.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.outter.user.UserInfoServerQuery;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.team.entity.TeamMemberVO;
import com.yonyou.pmclouds.team.mapper.TeamMapper;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = TeamMemberQuery.class)
public class TeamMemberQueryImpl implements TeamMemberQuery {

    @Autowired
    private TeamMapper mapper;

    @Autowired
    private UserInfoServerQuery userInfoQuery;


    @Override
    public TeamMemberVO[] queryByProject(String projectid)
            throws BusinessException {
        return mapper.queryTeamByProject(projectid);
    }
    @Override
    public TeamMemberVO[] queryById(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids)) {
            return new TeamMemberVO[0];
        }
        return mapper.queryByIds(ids);
    }
	@Override
	public TeamMemberVO[] queryByPkUsers(String[] pkUsers) throws BusinessException {
		if (ArrayUtils.isEmpty(pkUsers)) {
            return new TeamMemberVO[0];
        }
        return mapper.queryByPkUsers(pkUsers);

	}

    @Override
    public TeamMemberVO[] queryByProjectAndEnts(String projectid, String[] entids) throws BusinessException {
        if(StringUtils.isEmpty(projectid) || ArrayUtils.isEmpty(entids))
            return new TeamMemberVO[0];

        return mapper.queryTeamByProjectAndEnts(projectid,entids);
    }


    @Override
    public TeamMemberVO[] queryByProAndPkUsers(String projectid, String[] pkUsers) throws BusinessException {
        if (ArrayUtils.isEmpty(pkUsers)) {
            return new TeamMemberVO[0];
        }
        if(StringUtils.isEmpty(projectid)){
            this.queryByPkUsers(pkUsers);
        }

        return mapper.getByProjectAndUsers(projectid, SqlUtil.getSqlIn(pkUsers));

    }

    @Override
    public String[] queryExistMember(String projectid, String[] pkUsers) throws BusinessException {
        return  mapper.queryExistMember(projectid, pkUsers);
    }

    @Override
    public int countProjectMembers(String projectid) throws BusinessException {
        if(StringUtils.isEmpty(projectid)){
            return 0;
        }
        int result = mapper.countProjectMembers(projectid);
        return result;
    }

    @Override
    public Map<String, TeamMemberVO> queryMembersByEntAndProjects(String tenantId, String entId, String[] projectIds) throws BusinessException {
        if (StringUtils.isEmpty(entId) || StringUtils.isEmpty(tenantId)) {
            return null;
        }
        TeamMemberVO[] teamMemberVOS = mapper.queryMembersByEntAndProjects(tenantId, entId, projectIds);
        if (ArrayUtils.isEmpty(teamMemberVOS)) {
            return null;
        }
        Map<String, TeamMemberVO> results = new HashMap<>(teamMemberVOS.length);
        for (TeamMemberVO vo : teamMemberVOS
                ) {
            results.put(vo.getPkUser() + vo.getPkProject(), vo);
        }

        return results;
    }

    @Override
    public List<TeamMemberVO> queryByUserAndTenant(String pkUser, String pkTenant) throws BusinessException {
        return mapper.queryByUserAndTenant(pkUser, pkTenant);
    }

    @Override
    public TeamMemberVO[] queryByprojects(String[] pkProjects) throws BusinessException {
        if (ArrayUtils.isEmpty(pkProjects)) {
            return new TeamMemberVO[0];
        }

        return mapper.queryByprojects(pkProjects);
    }

    @Override
    public TeamMemberVO[] queryByEnterprise(String pkTenant, String pkEnterprise) throws BusinessException {
        if (StringUtils.isEmpty(pkEnterprise))
            return new TeamMemberVO[0];

        if (StringUtils.isEmpty(pkTenant))
            pkTenant = RuntimeEnvironment.getTenantId();

        return mapper.queryByEnterprise(pkTenant, pkEnterprise);
    }

    /**
     * 查询同时参与多个项目的user
     * @param pkTenant
     * @param pkProjects
     * @param start
     * @param offset
     * @return
     */
    public String[] queryInnerSecUserByPorjects(String pkTenant, String[] pkProjects, int start, int offset){
        if (StringUtils.isEmpty(pkTenant) || pkProjects == null || pkProjects.length == 0)
            return new String[0];
        return mapper.queryInnerSecUserByPorjects(pkTenant, pkProjects, pkProjects.length, start, offset);
    }

    /**
     * 查询同时参与多个项目user数量
     * @param pkTenant
     * @param pkProjects
     * @param start
     * @param offset
     * @return
     */
    public Integer countInnerSecUserByPorjects(String pkTenant, String[] pkProjects, int start, int offset){
        if (StringUtils.isEmpty(pkTenant) || pkProjects == null || pkProjects.length == 0)
            return 0;
        return mapper.countInnerSecUserByPorjects(pkTenant, pkProjects, pkProjects.length, start, offset);
    }

    @Override
    public PageObject<Object> getInnerSectionInfoByProjects(String[] pkProjects, int start, int offset) {

        Integer count = countInnerSecUserByPorjects(RuntimeEnvironment.getTenantId(), pkProjects, start, offset);
        String[] pkusers = queryInnerSecUserByPorjects(RuntimeEnvironment.getTenantId(), pkProjects, start, offset);
        if(count == null || count == 0 || pkusers == null || pkusers.length == 0){
            return new PageObject<Object>(new UserInfoRefVO[0], 0, 0);
        }
        Map<String, String> userMap = userInfoQuery.queryUserNames(pkusers);
        if (userMap == null && userMap.size() == 0){
            return new PageObject<Object>(new UserInfoRefVO[0], 0, 0);
        }
        List<UserInfoRefVO> userInfoRefVOList = new ArrayList<UserInfoRefVO>();
        for (String key : userMap.keySet()) {
            UserInfoRefVO userInfoRefVO = new UserInfoRefVO();
            userInfoRefVO.setUserId(key);
            userInfoRefVO.setUserName(userMap.get(key));
            userInfoRefVOList.add(userInfoRefVO);
        }
        return new PageObject<Object>(userInfoRefVOList.toArray(new UserInfoRefVO[0]), count, start);
    }

}
@Data
class UserInfoRefVO {

    private String userId;
    private String userName;

}